package xyz.erupt.flow.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import xyz.erupt.upms.model.EruptOrg;

import java.util.List;

public interface EruptOrgRepository extends JpaRepository<EruptOrg, Long> {

    /**
     * 查询上级为空的单位
     * @return
     */
    List<EruptOrg> findByParentOrgIdIsNullOrderBySortAsc();

    /**
     * 查询上级为空的单位
     * @return
     */
    @Query("from EruptOrg where isnull(parentOrg.id) = 1 and (name like concat('%',:keyword,'%') or code like concat('%',:keyword,'%') ) ")
    List<EruptOrg> findByParentOrgIdIsNullOrderByKeywordSortAsc(@Param("keyword") String keyword);

    /**
     * 根据上级机构id查询
     * @param orgId
     * @return
     */
    List<EruptOrg> findByParentOrgIdOrderBySortAsc(Long orgId);

    /**
     * 根据上级机构id查询
     * @param orgId
     * @return
     */
    @Query("from EruptOrg where parentOrg.id=:orgId and (name like concat('%',:keyword,'%') or code like concat('%',:keyword,'%') )")
    List<EruptOrg> findByParentOrgIdOrderByKeywordSortAsc(@Param("orgId") Long orgId, @Param("keyword") String keyword);
}
